package com.qianniu.napi.admin.service.akka;

import akka.actor.UntypedActor;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.entity.penum.*;
import com.qianniu.napi.admin.service.*;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.DateUtil;
import com.qianniu.napi.common.util.SpringUtil;
import com.qianniu.napi.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

public class SmsActor extends UntypedActor {

    private final static Logger logger = LoggerFactory.getLogger("actorlog");

    private IUserService userService = (IUserService) SpringUtil.getBean("userServiceImpl");
    private IUserstatService userstatService = (IUserstatService) SpringUtil.getBean("userstatServiceImpl");
    private IFensService fensService = (IFensService) SpringUtil.getBean("fensServiceImpl");
    private IRedisService redisService = (IRedisService) SpringUtil.getBean("redisServiceImpl");
    private IJifenService jifenService = (IJifenService) SpringUtil.getBean("jifenServiceImpl");
    private IJifenFlowService jifenFlowService = (IJifenFlowService) SpringUtil.getBean("jifenFlowServiceImpl");
    private IAccountService accountService = (IAccountService) SpringUtil.getBean("accountServiceImpl");
    private IAccountFlowService accountFlowService = (IAccountFlowService) SpringUtil.getBean("accountFlowServiceImpl");
    private IMconfigExtService mconfigExtService = (IMconfigExtService) SpringUtil.getBean("mconfigExtServiceImpl");
    private IMaccountService maccountService = (IMaccountService) SpringUtil.getBean("maccountServiceImpl");
    private IOrderService orderService = (IOrderService) SpringUtil.getBean("orderServiceImpl");
    private ISmslogService smslogService = (ISmslogService) SpringUtil.getBean("smslogServiceImpl");

    @Override
    public void onReceive(Object message) {

        String msg = message.toString();
        logger.debug(">>>>>> FanActor receive message:" + msg);

        // 格式 type+uid+mpid
        String[] arr = msg.split("@");
        if (arr.length < 3) return;
        try {

            String type = arr[0];
            String uid = arr[1];
            String mpid = arr[2];
//            String ywid = arr.length>3? arr[3]:null;
            switch (type) {
                case Constants.AKKA_MSG_SMS_YUYUE:
                    yuyueSms(Long.valueOf(uid), Long.valueOf(mpid));
                    break;
                default:
                    return;
            }

        } catch (Exception e) {
            logger.error("----------------error happen", e);
        }
    }

    // 自己注册奖励
    private void yuyueSms(Long uid, Long oid) {
        logger.info("---------------------------------------------------------yuyueSms come in");
        Order order = orderService.selectById(oid);
        Long sid = order.getSid();
        Long mid = order.getMid();
        Long mpid = order.getMpid();

        if (sid == null) {
            logger.info("---------------------------------------------------------yuyueSms sid==null");
            return;
        }
        Store store = redisService.getStore(sid);
        String kftel = store.getPhone();

        User muser = userService.selectOne(new EntityWrapper<User>()
                .setSqlSelect("id,phone")
                .eq("sid", sid).eq("is_mch", 1).eq("is_admin", 1));

        if (muser == null) {
            logger.info("---------------------------------------------------------yuyueSms sid==null");
            return;
        }
        String adminphone = muser.getPhone();

        MconfigExt mconfigExt = mconfigExtService.selectById(order.getMpid());
        Maccount ma = maccountService.selectOne(new EntityWrapper<Maccount>().eq("mid", mid));

        logger.info("--------------------------------------ma.getSmstotal()=" + ma.getSmstotal());
        logger.info("--------------------------------------mconfigExt=" + JSON.toJSONString(mconfigExt));
        logger.info("--------------------------------------kftel=" + kftel);
        logger.info("--------------------------------------adminphone=" + adminphone);

        if (ma.getSmstotal().intValue() <= 0) {
            logger.info("---------------------------------------------------------yuyueSms ma.smstotal no left");
            return;
        }
        if (mconfigExt == null || mconfigExt.getSmstplidYuyueUser() == null || mconfigExt.getSmstplidYuyueMch() == null) {
            logger.info("---------------------------------------------------------yuyueSms mconfigExt not config for yuyue");
            return;
        }
        //给用户发送短信
        //华口云看牙平台提醒您，您于%s预约了%s的看牙服务，门诊地址：%s，门诊电话：%s，请准时就诊！
        String sms = String.format("华口云看牙平台提醒您，您于%s预约了%s的看牙服务，门诊地址：%s，门诊电话：%s，请准时就诊！",
                order.getYytime(), store.getName(), store.getAddress(), store.getPhone());
        String[] param = {order.getYytime(), store.getName(), store.getAddress(), store.getPhone()};

        smslogService.sendOnetplsms(order.getPhone(), sms, param, mconfigExt.getSmstplidYuyueUser(), mid, mpid,sid,oid);

        //给商户发送短信
        //尊敬的s%您好，客户s%于s%预约贵门诊的看牙服务，请准时接待！
        //尊敬的s%您好，客户s%于s%预约了贵门诊的看牙服务，请准时接待！
        sms = String.format("尊敬的%s您好，客户%s于%s预约了贵门诊的看牙服务，请准时接待！",
                store.getName(), order.getLinkman() + order.getPhone(), order.getYytime());
        String[] param2 = {store.getName(), order.getLinkman() + order.getPhone(), order.getYytime()};

        smslogService.sendOnetplsms(adminphone, sms, param2, mconfigExt.getSmstplidYuyueMch(), mid, mpid,sid,oid);

    }

}
